﻿using System.Net.Http.Json;
using BaseData.CommonLib.Constants;
using BaseData.CommonLib.Models;
using Newtonsoft.Json;
using Swimj.CommonLib.Base;
using Swimj.Core.Extensions;

namespace BaseData.RazorLib.Services;

public class SaleOrderService : ISaleOrderService
{
    private readonly IHttpClientFactory _httpClientFactory;

        public SaleOrderService(IHttpClientFactory httpClientFactory)
        {
            _httpClientFactory = httpClientFactory;
        }

        public async Task<PaginatedResult<SaleOrderInfoResponseDto>?> QuerySaleOrdersByPage(SaleOrderSearchRequestDto searchRequestDto)
        {
            try
            {
                var httpClient = _httpClientFactory.CreateClient("basedataApi");
                var response = await httpClient.GetAsync(
                    searchRequestDto.ToQueryString(ApiRoutes.SaleOrder.QuerySaleOrders));
                if (response.IsSuccessStatusCode)
                {
                    var resultJson = await response.Content.ReadAsStringAsync();

                    if (!string.IsNullOrEmpty(resultJson))
                    {
                        return JsonConvert.DeserializeObject<PaginatedResult<SaleOrderInfoResponseDto>>(resultJson);
                    }
                }

                return null;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        public async Task<Result<SaleOrderInfoResponseDto>?> CreateSaleOrder(SaleOrderEditRequestDto model)
        {
            try
            {
                var httpClient = _httpClientFactory.CreateClient("basedataApi");
                var response =
                    await httpClient.PostAsJsonAsync($"/{ApiRoutes.SaleOrder.CreateSaleOrder}", model);
                if (response.IsSuccessStatusCode)
                {
                    var resultJson = await response.Content.ReadAsStringAsync();

                    if (!string.IsNullOrEmpty(resultJson))
                    {
                        return JsonConvert.DeserializeObject<Result<SaleOrderInfoResponseDto>>(resultJson);
                    }
                }

                return new Result<SaleOrderInfoResponseDto>();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        public async Task<Result<SaleOrderInfoResponseDto>?> GetSaleOrder(int? key)
        {
            try
            {
                var httpClient = _httpClientFactory.CreateClient("basedataApi");
                var response = await httpClient.GetAsync($"/{ApiRoutes.SaleOrder.GetSaleOrder.Replace("{id}", key.ToString())}");
                if (response.IsSuccessStatusCode)
                {
                    var resultJson = await response.Content.ReadAsStringAsync();

                    if (!string.IsNullOrEmpty(resultJson))
                    {
                        return JsonConvert.DeserializeObject<Result<SaleOrderInfoResponseDto>>(resultJson);
                    }
                }

                return null;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        public async Task<Result<SaleOrderInfoResponseDto>?> UpdateSaleOrder(int id, SaleOrderEditRequestDto editRequestDto)
        {
            try
            {
                var httpClient = _httpClientFactory.CreateClient("basedataApi");
                var response = await httpClient.PutAsJsonAsync($"/{ApiRoutes.SaleOrder.UpdateSaleOrder.Replace("{id}", id.ToString())}", editRequestDto);
                if (response.IsSuccessStatusCode)
                {
                    var resultJson = await response.Content.ReadAsStringAsync();

                    if (!string.IsNullOrEmpty(resultJson))
                    {
                        return JsonConvert.DeserializeObject<Result<SaleOrderInfoResponseDto>>(resultJson);
                    }
                }

                return new Result<SaleOrderInfoResponseDto>();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
}